# @Time : 2025-01-02 17:06
# @Author : Fioman 
# @Phone : 13149920693
"""
发送消息到web的封装,将封装的消息内容放到和web通信的队列中去,然后队列循环的任务,会发送
对应消息到前端,这里的消息封装和前端要一一对应起来.
"""
import json
from typing import Dict

from app_config import webMsgQueue
from eap_tool import table_access
from eap_tool.eap_logger import debug_eap
from personal_tool.data_result import DataResult
from personal_tool.enum_data_eap import EapMsgType
from web_tool.modal_config import NotificationConfig


def eap_data_update_to_web(dataType: EapMsgType) -> DataResult:
    """
    更新eap数据到web
    :param dataType:
    :return:
    """
    msgType = "UpdateEapModel"
    result = DataResult()
    try:
        match dataType:
            case EapMsgType.PcDataCraft:
                data = table_access.pc_data_craft_fetch_all()
            case EapMsgType.PcDataManual:
                data = table_access.pc_data_manual_fetch_all()
            case EapMsgType.PcDataEmp:
                data = table_access.pc_data_emp_fetch_all()
            case EapMsgType.PcDataStatus:
                data = table_access.pc_data_status_fetch_all()
            case EapMsgType.PcDataCount:
                data = table_access.pc_data_count_fetch_all()
            case EapMsgType.PcSignalStatus:
                data = table_access.pc_signal_status_fetch_all()
            case EapMsgType.EapSignalStatus:
                data = table_access.eap_signal_status_fetch_all()
            case _:
                data = []

        dataSend = {
            dataType.value: data
        }
        send_msg_to_web(msgType, dataSend)
        result.state = True
    except Exception as e:
        result.info = f"in eap_data_update_to_web() error: {e} "
        debug_eap(result.info)
    return result


def send_msg_to_web(msgType, data: Dict):
    """
    封装消息到前端,第一个参数是消息的类型,第二个是消息的内容
    :param msgType:
    :param data: 以字典的形式返回给前端数据,字典的键需要和前端的要更新的数据保持一致,这个怎么保证呢
                           只能认为的去约束了,编写代码的时候先写前端,后写后端
    :return:
    """
    webMsgQueue.put(json.dumps({msgType: data}))


def show_modal_in_web(modalConfig: dict):
    """
    发送弹窗配置到前端
    :param modalConfig: 弹窗配置,这个配置要和前端的参数相对应
    """
    msgType = "ShowModal"
    send_msg_to_web(msgType, modalConfig)


def show_notification_in_web(title: str, content: str, visible: bool = True):
    """
    显示通知弹窗
    """
    NotificationConfig["content"] = content
    NotificationConfig["title"] = title
    NotificationConfig["visible"] = visible
    show_modal_in_web(NotificationConfig)


def show_error_info_in_web(error_info: str):
    """
    显示错误信息弹窗
    """
    modal_config = {
        "type": "ERROR_INFO",
        "title": "错误提示",
        "errorInfo": error_info,
        "visible": True
    }
    show_modal_in_web(modal_config)


if __name__ == '__main__':
    print(123)
